package com.felton.springboot.shrio.service.impl;

import com.felton.springboot.shrio.model.LoginResult;
import com.felton.springboot.shrio.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;


/**
 * 类  名：com.felton.springboot.shrio.service.impl.LoginServiceImpl
 * 类描述：todo
 * 创建人：liurui
 * 创建时间：2020/6/10 11:08
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author liurui
 * @version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Override
    public LoginResult login(String userName, String password) {
        LoginResult result = new LoginResult();
        if (userName == null || userName.isEmpty()) {
            result.setLogin(false);
            result.setResult("用户名为空");
            return result;
        }

        String msg = "";



        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.login(token);
            Session session = currentUser.getSession();
            session.setAttribute("userName", userName);
            result.setLogin(true);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setLogin(false);
        result.setResult("");

        return result;
    }

    @Override
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }
}
